package com.pureut.system.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.system.domain.PrintDictionary;
import com.pureut.system.domain.SysPrintTemplate;
import com.pureut.system.domain.dto.SysPrintTemplateDto;
import com.pureut.system.domain.vo.SysPrintTemplateVo;
import com.pureut.system.service.ISysPrintTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 打印模板控制层
 * @Date 23-03-15
 * @Author hl
 **/
@RestController
@RequestMapping("/printConfig")
public class SysPrintTemplateController extends BaseController {

    @Resource
    private ISysPrintTemplateService sysPrintTemplateService;


    /**
     * 获取打印模板页面
     */
    @RequiresPermissions("system:printConfig:list")
    @GetMapping("/list")
    public TableDataInfo list(SysPrintTemplateVo sysPrintTemplateVo)
    {
        startPage();
        List<SysPrintTemplateDto> list = sysPrintTemplateService.selectPrintTemplateList(sysPrintTemplateVo);
        return getDataTable(list);
    }

    /**
     * 新增打印模板
     */
    @RequiresPermissions("system:printConfig:add")
    @Log(title = "打印模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPrintTemplate SysPrintTemplate) {
        return toAjax(sysPrintTemplateService.insertPrintTemplate(SysPrintTemplate));
    }

    /**
     * 根据编号获取详细信息
     */
    @RequiresPermissions("system:printConfig:query")
    @GetMapping(value = "/{printId}")
    public AjaxResult getPrintTemplateInfo(@PathVariable long printId)
    {
        return AjaxResult.success(sysPrintTemplateService.selectPrintTemplateById(printId));
    }

    /**
     * 点击配置获取JSON数据
     */
    @RequiresPermissions("system:printConfig:config")
    @GetMapping(value = "/config/{printId}")
    public AjaxResult getPrintTemplateConfig(@PathVariable long printId)
    {
        return AjaxResult.success(sysPrintTemplateService.getPrintTemplateConfig(printId));
    }

    /**
     * 删除打印模板
     */
    @RequiresPermissions("system:printConfig:remove")
    @Log(title = "打印模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{printId}")
    public AjaxResult remove(@PathVariable("printId") String printId) {
        return toAjax(sysPrintTemplateService.deletePrintTemplate(printId));
    }

    /**
     * 修改打印模板
     */
    @RequiresPermissions("system:printConfig:edit")
    @Log(title = "打印模板", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysPrintTemplate sysPrintTemplate) {
        return toAjax(sysPrintTemplateService.updatePrintTemplateEdit(sysPrintTemplate));
    }

    /**
     * 获取模板字段
     */
    @GetMapping(value = "/details/{printCorrelation}")
    public AjaxResult getPrintTemplateDetails(@PathVariable long printCorrelation)
    {
        return AjaxResult.success(sysPrintTemplateService.getPrintTemplateDetails(printCorrelation));
    }

    /**
     * 通过模板类型查对应的模板列表
     */

    @GetMapping("/typeList")
    public AjaxResult typeList(int type)
    {
        List<SysPrintTemplateDto> list = sysPrintTemplateService.selectList(type);
        return AjaxResult.success(list);
    }

    /**
     * 通过模板id新增字段
     */
    @PostMapping("/dictionary")
    public AjaxResult dictionary(@RequestBody List<PrintDictionary> list)
    {
        return toAjax(sysPrintTemplateService.insertPrintDictionary(list));
    }


}
